﻿CREATE PROCEDURE [dbo].[Articles_InsertFromTempArticles]
	@RangeReviewId int
AS
	INSERT INTO Articles(RangeReviewId, CategoryId, Code, Name, EAN, UOM, Brand, Manufacturer, Delist, IsDelisted, IsDeletedRangeReview, IsDeletedPerformance, IsNew, TaxRate, Size, Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, Price, CreatedDate, Width, Height, Facings, Type, OrderUOM, OrderEAN, Pack, Vendor, NetPrice, Cost, Source)
	SELECT distinct RangeReviewId, CategoryId, Code, Name, EAN, UOM, Brand, Manufacturer, 0, 0, 0, 0, 0, ISNULL(TaxRate,0), Size, Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, Price, CreatedDate, Width, Height, Facings, Type, OrderUOM, OrderEAN, Pack, Vendor, NetPrice, Cost, 'ArticleMaster'
	FROM TempArticles temp
	WHERE temp.RangeReviewId = @RangeReviewId and
	NOT EXISTS
		(select * 
		 from Articles art
		 where RangeReviewId = @RangeReviewId and
			   art.Code = temp.Code and
			   ISNULL(art.UOM,0) = ISNULL(temp.UOM,0))
RETURN 0
